<?php

namespace App\Http\Controllers\Api\V5;

use App\Http\Controllers\Controller;
use App\Models\V1\AppUserMessageModel;
use App\Models\V1\MemberModel;
use Dingo\Api\Routing\Helpers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use DB;

class AppUseMessageController extends Controller
{
    use Helpers;

    /**
     * @var AppUserMessageModel
     */
    private $appUserMessageModel;

    public function __construct(AppUserMessageModel $appUserMessageModel)
    {
        $this->appUserMessageModel = $appUserMessageModel;
    }

    /**
     * 保存APP的使用的情况
     * @param Request $request
     * @return mixed
     */
    public function use_device_info(Request $request)
    {
        $mobile      = $request->mobile;
        $key         = $request->key;
        $openAppDate = $request->openAppDate;//用户打开app的时间
        $phoneSystem = $request->phoneSystem;//用户的手机的系统版本信息
        $appVersion  = $request->appVersion;//app的版本信息
        $status      = $this->check_user($request->all());
        $message     = isset($status) ? $status->messages() : '';
        if ($status->fails()) {
            return apiData()->send(403,'参数错误');
        }
        $memberinfo = new MemberModel();
        $userInfo   = $memberinfo->get_member_info_by_mobile_key($mobile, $key);//用户提交用户的信息
        if (!$userInfo) {
            return apiData()->send(404,'用户信息不存在');
        }
        $userId = $userInfo->UserID;

        $status  = $this->appUserMessageModel->store($userId, $openAppDate, $phoneSystem, $appVersion);//保存用户的信息
        $message = isset($status) ? '保存成功' : '保存失败';
        return apiData()->send(200,$message);
    }


    public function check_user($parm)
    {
        $data = Validator::make($parm,
            [
                'mobile'      => 'required',
                'key'         => 'required',
                'openAppDate' => 'required',
                'phoneSystem' => 'required',
                'appVersion'  => 'required'
            ],
            [
                'mobile.required'      => '手机号不能为空',
                'key.required'         => '设备号不能为空',
                'openAppDate.required' => '用户开启app的时间不能为空',
                'phoneSystem.required' => '手机的系统不能为空',
                'appVersion.required'  => 'app的版本号不能为空'
            ]
        );

        return $data;
    }




    /**
     * app使用日志
     * */
    public function member_online_log(Request $request)
    {
        $id         = $request->input('id',0);  //日志ID
        $userId     = $request->input('userId',0);
        $key        = $request->input('key','');
        $time       = date_time();
        $db         = DB::table('member_online_log');
        $logInfo    = $id > 0 ? $db->where('id',$id)->first() : null; //获取信息计算在线时间长度

        if($logInfo)
        {
            $timeLength = time()-strtotime($logInfo->begin_time);
            $data   = [
                'end_time'      => $time,
                'time_length'   => $timeLength
            ];
            if($userId > 0)
            {
                $data['user_id']    = $userId;
            }

            if($userId == $logInfo->user_id || ($logInfo->user_id == 0 && $userId > 0))
            {
                $db->where('id',$id)->update($data);
            }
            else
            {
                $id = 0;
            }
        }else{
            $id = 0;
        }

        if($id == 0 )
        {
            $data   = [
                'user_id'       => $userId,
                'begin_time'    => $time,
                'end_time'      => $time,
                'time_length'   => 0,
                'ip'            => get_real_ip(),
                'key'           => $key
            ];
            $id = $db->insertGetId($data);
        }
        return apiData()->set_data('id',$id)->send(200,'ok');
    }
}